package com.example.wacoffee_manage.Service.Impl;

import com.example.wacoffee_manage.Mapper.SupplyMapper;
import com.example.wacoffee_manage.Service.supplyService;
import com.example.wacoffee_manage.common.PageResult;
import com.example.wacoffee_manage.pojo.Good;
import com.example.wacoffee_manage.pojo.Supply;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class supplyServiceImpl implements supplyService {
    @Autowired
    private SupplyMapper supplyMapper;


    @Override
    public PageResult getAll(Supply supply) {
        PageHelper.startPage(supply.getPage(),supply.getPageSize());
        List<Supply> supplyList=supplyMapper.getAll(supply);
        Page<Supply> pages=(Page<Supply>) supplyList;
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    @Override
    public List<Good> getProductsBySupplyId(Integer id) {
        return supplyMapper.getProductsBySupplyId(id);
    }

    @Override
    public void addStock(Integer id, Integer productId, Integer stock) {
        supplyMapper.addStock(id,productId,stock);
    }

    @Override
    public Supply getSupplyById(Integer id) {
        return supplyMapper.getSupplyById(id);
    }

    @Override
    public void update(Supply supply) {
         supplyMapper.update(supply);
    }

    @Override
    public void delete(Integer id) {
        if(supplyMapper.judgeSupplyAppearGood(id)==0) supplyMapper.delete(id);
        else throw new RuntimeException("该供应商有商品，不能删除");
    }

}
